package com.yyj.controller;

import com.github.pagehelper.PageInfo;
import com.yyj.entity.Dept;
import com.yyj.entity.DeptParams;
import com.yyj.entity.Result;
import com.yyj.service.DeptService;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("/dept")
public class DeptController {

    final
    DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }


    //增加管理员信息和编辑管理员信息
    @PostMapping
    public Result insertDept(@RequestBody Dept dept) {

        if (dept.getId() == null) {
            deptService.insertDept(dept);
        } else {
            //修改
            deptService.updateDept(dept);
        }
        return Result.success();
    }


    @GetMapping("/search")
    public Result getBySearch(DeptParams deptParams) {
        PageInfo<Dept> info = deptService.getBySearch(deptParams);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        deptService.delete(id);
        return Result.success();
    }

    @GetMapping("/getAll")
    public Result getAll() {
        List<Dept> list = deptService.getDept();
        return Result.success(list);
    }
}
